<?php
/**
 * 图片素材控制器
 */
namespace Admin\Controller;
use Think\Controller;
class ImageController extends InitController {
    
	// 图片列表
	public function lists(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$start_time = strtotime(I('start_time'));
		$end_time = strtotime(I('end_time'));

		$model = M('ImageMaterial');

		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间 
				$where['createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间 
				$where['createtime'] = array('ELT', $end_time);
			}
		}

		$total =$model->where($where)->count('id');

		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		
		$list = $model->where($where)->limit("{$Page->firstRow},{$Page->listRows}")->order("id desc")->select();


		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('lists');
	}

	//图片添加
	public function imgadd(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];


		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('imgadd');
	}

	//图片添加动作
	public function do_imgadd(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		if(!IS_AJAX){
			return false;
		}
		
		$img_path = I('newspicture');

		foreach($img_path as $v){
			$path = M('ImageMaterial')->where(array("img_path"=>$v))->find();

			if(empty($path)){
				$data = array(
					'img_path'=>$v,
					'createtime'=>NOW_TIME
				);

				$id[] = M('ImageMaterial')->add($data);
			}
		}

		if(!empty($id)){
			$this->success('操作成功');
		}else{
			$this->success('操作失败');
		}
	}


	//图片删除
	public function imagedel(){
		$id = I('id');

		if(empty($id)){
			return false;
		}

		$bool = M('ImageMaterial')->where(array('id'=>$id))->delete();

		if($bool){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}

	}



	

	
}